#include "QueueFamilies.h"

mini_engine::QueueFamilies *mini_engine::QueueFamilies::m_queueFamilies = nullptr;
mini_engine::QueueFamilyIndices mini_engine::QueueFamilies::queueFamilyIndices;

mini_engine::QueueFamilies::QueueFamilies()
{
}

mini_engine::QueueFamilyIndices mini_engine::QueueFamilies::findQueueFamilyIndices(VkPhysicalDevice device)
{
    QueueFamilyIndices indices;

    uint32_t queueFamilyCount = 0;
    vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, nullptr);

    std::vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);
    vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, queueFamilies.data());

    int i = 0;
    for (const auto &queueFamily : queueFamilies)
    {
        if (queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT)
        {
            indices.graphicsFamily = i;
        }

        VkBool32 presentSupport = false;
        vkGetPhysicalDeviceSurfaceSupportKHR(device, i, WindowSurface::surface, &presentSupport);

        if (presentSupport)
        {
            indices.presentFamily = i;
        }

        if (indices.isComplete())
        {
            break;
        }

        i++;
    }

    return indices;
}

mini_engine::QueueFamilies *mini_engine::QueueFamilies::getQueueFamilies()
{
    if (m_queueFamilies == nullptr)
    {
        m_queueFamilies = new QueueFamilies();
        static QueueFamiliesDestruction queueFamiliesDestruction;
    }
    return m_queueFamilies;
}

mini_engine::QueueFamilies::~QueueFamilies()
{
}

mini_engine::QueueFamilies::QueueFamiliesDestruction::~QueueFamiliesDestruction()
{
    if (m_queueFamilies != nullptr)
    {
        delete m_queueFamilies;
        m_queueFamilies = nullptr;
    }
}
